package com.usernet.product.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.usernet.product.comm.BaseDao;
import com.usernet.product.entity.Resources;
import com.usernet.product.utils.Page;
import com.usernet.product.utils.PageResult;
import com.usernet.product.utils.ProductConfig;

public class ResourcesDao extends BaseDao {

	@SuppressWarnings("unchecked")
	public List<Resources> getAll() {
		String hql = "select new Resources(res.id,res.name) from Resources as res order by sort";
		return super.getAllByHql(hql);
	}

	/**
	 * 分页查询分类信息(从1开始)
	 */
	@SuppressWarnings("unchecked")
	public List<Resources> getAllTypeByPage(int pageSize, int pageNo) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();
			String hql = "select new Resources(res.id,res.name) from Resources as res where res.status!=? order by res.sort";
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS );
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			query.setCacheable(true);// 使用缓存
			List<Resources> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			tr.rollback();
			ex.printStackTrace();
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public List<Resources> getAllTypeForIndex(int pageSize, int pageNo) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();
			String hql = "select new Resources(res.id,res.name,icon) from Resources as res where res.status!=? order by res.sort";
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS );
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			query.setCacheable(true);// 使用缓存
			List<Resources> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			tr.rollback();
			ex.printStackTrace();
			return null;
		}
	}

	public int getCount() {
		Session session = getSession();
		Transaction tr = null;
		String hql = "select count(*) from Resources";
		try {
			tr = session.beginTransaction();
			Query query = session.createQuery(hql);
			query.setCacheable(true);// 使用缓存
			Object num = query.uniqueResult();
			tr.commit();
			if (num instanceof Number) {
				return ((Number) num).intValue();
			}
		} catch (HibernateException e) {
			if (tr != null) {
				tr.rollback();
			}
		}
		return 0;

	}
	@SuppressWarnings("unchecked")
	public PageResult<Resources> getAllByPage(Page page) {
		String hql = "from Resources order by sort";
		return super.getAllByPageAndHql(page, hql);
	}

	/*
	 * 条件查询
	 */
	@SuppressWarnings("unchecked")
	public PageResult<Resources> getAllByParams(Page page, String name) {
		String hql = "from Resources where 1=1 ";
		if (name != null && !"".equals(name.trim())) {
			hql += " and (name like '%" + name + "%' or pinyin like '%" + name.toLowerCase()
					+ "%')";
		}
		hql += " order by sort";
		return super.getAllByPageAndHql(page, hql);
	}
	
	// 专题总数
	public int getResourcesCount() {
		Session session = getSession();
		Transaction tr = null;
		String hql = "select count(*) from Resources as res where res.status!="
				+ ProductConfig.DIS_STATUS;
		try {
			tr = session.beginTransaction();
			Query query = session.createQuery(hql);
			query.setCacheable(true);// 使用缓存
			Object num = query.uniqueResult();
			tr.commit();
			if (num instanceof Number) {
				return ((Number) num).intValue();
			}
		} catch (HibernateException e) {
			if (tr != null) {
				tr.rollback();
			}
		}
		return 0;

	}

	// 专题
	@SuppressWarnings("unchecked")
	public List<Resources> getAllResources(int pageSize, int pageNo) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();
			String hql = "select new Resources(res.id,res.name,res.icon,res.remark) from Resources as res where res.status!="
					+ ProductConfig.DIS_STATUS + " order by res.sort";
			Query query = session.createQuery(hql);
			query.setCacheable(true);// 使用缓存
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			List<Resources> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			tr.rollback();
			ex.printStackTrace();
			return null;
		}
	}
}
